package indi.yangshenhui.spring.controller;

import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import indi.yangshenhui.hbase.dao.HBaseHelper;
import indi.yangshenhui.hbase.dao.constant.HBaseConstant;
import indi.yangshenhui.hbase.dao.entity.clusters.HBaseClustersStatusEntity;
import indi.yangshenhui.hbase.dao.pool.HTablePoolEngine;
import indi.yangshenhui.spring.constant.PropertiesConstant;
import indi.yangshenhui.tool.PropertiesUtil;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class SignInController {
	private static final Logger LOGGER = Logger
			.getLogger(SignInController.class);
	private static Properties properties = PropertiesUtil.getInstance()
			.getProperties("signin.properties");

	@RequestMapping("signin.do")
	public String signIn(@RequestParam(value = "username") String username,
			@RequestParam(value = "password") String password,
			HttpServletRequest request, HttpServletResponse response) {
		LOGGER.info("username = [" + username + "]");
		LOGGER.info("password = [" + password + "]");
		if (username.equals(properties.getProperty("username"))
				&& password.equals(properties.getProperty("password"))) {
			try {
				HTablePoolEngine.createHBaseConfiguration(
						HBaseConstant.HBASE_DEFAULT_INSTANCE,
						PropertiesConstant.HBASE_PROPERTIES
								.getProperty("hbase.zookeeper.quorum"),
						PropertiesConstant.HBASE_PROPERTIES.getProperty(""
								+ "hbase.zookeeper.property.clientport"));
				HBaseClustersStatusEntity hBaseClustersStatusEntity = HBaseHelper
						.getHBaseClustersStatus(HBaseConstant.HBASE_DEFAULT_INSTANCE);
				request.setAttribute("hBaseClustersStatusEntity",
						hBaseClustersStatusEntity);
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			}
			return "hbase_manage";
		}
		request.setAttribute("error", "username or password is error");
		return "sign_in";
	}

	@RequestMapping("home.do")
	public String home(HttpServletRequest request, HttpServletResponse response) {
		try {
			HTablePoolEngine.createHBaseConfiguration(
					HBaseConstant.HBASE_DEFAULT_INSTANCE,
					PropertiesConstant.HBASE_PROPERTIES
							.getProperty("hbase.zookeeper.quorum"),
					PropertiesConstant.HBASE_PROPERTIES.getProperty(""
							+ "hbase.zookeeper.property.clientport"));
			HBaseClustersStatusEntity hBaseClustersStatusEntity = HBaseHelper
					.getHBaseClustersStatus(HBaseConstant.HBASE_DEFAULT_INSTANCE);
			request.setAttribute("hBaseClustersStatusEntity",
					hBaseClustersStatusEntity);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		return "hbase_manage";
	}

	@RequestMapping("droptable.do")
	public String dropTable(@RequestParam(value = "tn") String tableName,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			HTablePoolEngine.createHBaseConfiguration(
					HBaseConstant.HBASE_DEFAULT_INSTANCE,
					PropertiesConstant.HBASE_PROPERTIES
							.getProperty("hbase.zookeeper.quorum"),
					PropertiesConstant.HBASE_PROPERTIES.getProperty(""
							+ "hbase.zookeeper.property.clientport"));
			boolean flag = HBaseHelper.drop(tableName,
					HBaseConstant.HBASE_DEFAULT_INSTANCE);
			if (flag) {
				response.getWriter().write("true");
			}
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		return null;
	}

	@RequestMapping("forward.do")
	public String forward(@RequestParam(value = "forward") String forward) {
		return forward;
	}

}